var express = require("express");
var router = express.Router();
const axios = require("axios");

const morningData = require("../data/morning");

/* GET users listing. */
router.get("/list", function (req, res, next) {
  res.json({
    success: true,
    data: morningData,
  });
});

router.get("/getAdCode", (req, res, next) => {
  const { location } = req.query;

  if (location) {
    axios
      .get(
        `https://restapi.amap.com/v3/geocode/regeo?key=3195001c4e6a22f61af642890defaf21&location=${location}`
      )
      .then((response) => {
        res.json({
          success: true,
          data: response.data,
        });
      });
  } else {
    res.json({
      success: false,
      msg: "参数错误",
    });
  }
});

router.get("/getWeather", (req, res, next) => {
  const { adcode } = req.query;

  if (adcode) {
    axios
      .get(`https://i.news.qq.com/i/getWeather?adcode=${adcode}&from_scene=6`)
      .then((response) => {
        res.json({
          success: true,
          data: response.data,
        });
      });
  } else {
    res.json({
      success: false,
      msg: "参数错误",
    });
  }
});

module.exports = router;
